package cn.edu.lcu.cs.javaprogramming.collection;

import lombok.Getter;
import lombok.Setter;

/**
 * <p>
 * 在HashSet集合中添加三个Person对象，把姓名相同的人当作同一个人，禁止重复添加.
 * </p>
 * <ol>
 * <li>Person类中定义name与age属性，重写hashCode()方法和equals()方法，针对Person类的name属性进行比较。</li>
 * <li>如果name相同，hashCode()方法的返回值相同，equals()方法返回true。</li>
 * </ol>
 *
 * @author Ling
 */
@Getter
@Setter
public class Person implements Comparable<Person> {
    private int age;
    private String name;

    public Person(String name, int age) {
        this.age = age;
        this.name = name;
    }

    @Override
    public int hashCode() {
        return name.hashCode();
    }

    @Override
    public String toString() {
        return "{name:" + name + ", age:" + age + "}";
    }

    @Override
    public int compareTo(Person target) {
        return this.age - target.age;
    }
}
